package com.wkedu.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkedu.repair.dao.TeacherMapper;
import com.wkedu.repair.dto.TeacherDto;
import com.wkedu.repair.entity.Teacher;
import com.wkedu.repair.service.TeacherService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/9/5 10:24 上午
 */
@Transactional
@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public void addTeacher(TeacherDto teacherDto) {
        Teacher teacher = new Teacher();
        teacher.setTeacherId(teacherDto.getTeacherId());
        teacher.setCourse(teacherDto.getCourse());
        teacher.setMajor(teacherDto.getMajor());
        teacher.setName(teacherDto.getName());
        teacher.setPassword(teacherDto.getPassword());
        teacher.setUsername(teacherDto.getUsername());
        teacherMapper.insert(teacher);
    }

    @Override
    public TeacherDto getTeacherForTeacherId(String teacherId) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("teacher_id",teacherId);

        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        if (teacher != null){
            TeacherDto teacherDto = new TeacherDto(teacher.getName(), teacher.getTeacherId()
                    , teacher.getUsername(), teacher.getPassword(), teacher.getMajor(), teacher.getCourse());

            return teacherDto;
        }else {
            return null;
        }
    }

    @Override
    public TeacherDto getTeacherForUsername(String username) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("username",username);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        if (teacher != null){
            TeacherDto teacherDto = new TeacherDto(teacher.getName(), teacher.getTeacherId()
                    , teacher.getUsername(), teacher.getPassword(), teacher.getMajor(), teacher.getCourse());

            return teacherDto;
        }else {
            return null;
        }
    }
}
